********************************************
***************** Revenue Production Function *************
********************************************
foreach theta in by_ind_thetas_R_kl by_ind_thetas_R_kl_3lags by_ind_thetas_Q_kl  by_ind_thetas_CS_kl_nic4  by_ind_thetas_CS_kl_year_nic2 {
use   "$working/results/`theta'", clear


keep nic_08_2dig theta_l_hat_q_ind_mean theta_l_hat_q_ind_sd theta_k_hat_q_ind_mean theta_k_hat_q_ind_sd Observations 
la var Observations "Obs."
la var theta_l_hat_q_ind_mean `"\(\theta_l^q\)"'
la var theta_k_hat_q_ind_mean `"\(\theta_k^q\)"'
	replace nic_08_2dig = "10 Food" if nic_08_2dig == "10"
	replace nic_08_2dig = "13 Textiles" if nic_08_2dig== "13"
	replace nic_08_2dig = "17 Paper" if nic_08_2dig == "17"
	replace nic_08_2dig = "20 Chemicals" if nic_08_2dig == "20"
	replace nic_08_2dig = "21 Parmaceuticals" if nic_08_2dig == "21"
	replace nic_08_2dig = "22 Rubber and Plastic" if nic_08_2dig == "22"
	replace nic_08_2dig = "23 Non-metallic minerals" if nic_08_2dig == "23"
	replace nic_08_2dig = "24 Basic Metals" if nic_08_2dig == "24"
	replace nic_08_2dig = "25 Fabricated Metals" if nic_08_2dig == "25"
	replace nic_08_2dig = "26 Computer and Electronics" if nic_08_2dig == "26"
	replace nic_08_2dig = "27 Electrical Equipment" if nic_08_2dig == "27"
	replace nic_08_2dig = "28 Machinery" if nic_08_2dig == "28"
	replace nic_08_2dig = "29 Motor Vehicles" if nic_08_2dig == "29"

expand 3
sort nic_08_2dig
foreach v in  theta_l_hat_q_ind_mean  theta_k_hat_q_ind_mean   {
	replace `v' = . if `v' == `v'[_n-1] | `v' == `v'[_n-2]
	tostring `v', replace format(%9.3f) force
	replace `v' = "" if `v' == "."
	}

replace nic_08_2dig = "" if nic_08_2dig == nic_08_2dig[_n-1] | nic_08_2dig == nic_08_2dig[_n-2] 
replace Observations = . if Observations == Observations[_n-1] | Observations == Observations[_n-2]
tostring Observations, replace format(%9.0f) force
replace Observations = "" if Observations == "."

foreach v in theta_l_hat_q_ind_sd theta_k_hat_q_ind_sd  {
	replace `v' = . if !missing(nic_08_2dig)
	replace `v' = . if `v' == `v'[_n-1] 
	tostring `v', replace format(%9.3f) force
	replace `v' = "[" + `v' + "]"
	replace `v' = "" if `v' == "[.]"
	}

replace theta_l_hat_q_ind_mean = theta_l_hat_q_ind_sd if theta_l_hat_q_ind_mean == ""
replace theta_k_hat_q_ind_mean = theta_k_hat_q_ind_sd if theta_k_hat_q_ind_mean == ""
drop  theta_l_hat_q_ind_sd theta_k_hat_q_ind_sd  

texsave  nic_08_2dig theta_l_hat_q_ind_mean  theta_k_hat_q_ind_mean  Observations using "$tables/elasticities_`theta'.tex", ///
	footnote("Note: For each sector, the first row is the mean, second row, in brackets, is the standard deviation.") ///
	title("Output elasticities (`theta')") ///
	nofix replace location(h)  varlab marker(tab:elasticities_`theta') frag ///
	width(.6\textwidth)
}
********************************************
***************** Quantity Production Function *************
********************************************	
foreach theta in by_ind_thetas_Q_klme by_ind_thetas_Q_klme_2lags  by_ind_thetas_CS_klme_nic4  by_ind_thetas_CS_klme_year_nic2 by_ind_thetas_Q_klm {
use   "$working/results/`theta'", clear


keep nic_08_2dig theta_l_hat_q_ind_mean theta_l_hat_q_ind_sd theta_k_hat_q_ind_mean theta_k_hat_q_ind_sd theta_m_hat_q_ind_mean theta_m_hat_q_ind_sd theta_e_hat_q_ind_mean theta_e_hat_q_ind_sd Observations 
la var Observations "Obs."
la var theta_l_hat_q_ind_mean `"\(\theta_l^q\)"'
la var theta_k_hat_q_ind_mean `"\(\theta_k^q\)"'
la var theta_m_hat_q_ind_mean `"\(\theta_m^q\)"'
la var theta_e_hat_q_ind_mean `"\(\theta_e^q\)"'
	replace nic_08_2dig = "10 Food" if nic_08_2dig == "10"
	replace nic_08_2dig = "13 Textiles" if nic_08_2dig== "13"
	replace nic_08_2dig = "17 Paper" if nic_08_2dig == "17"
	replace nic_08_2dig = "20 Chemicals" if nic_08_2dig == "20"
	replace nic_08_2dig = "21 Parmaceuticals" if nic_08_2dig == "21"
	replace nic_08_2dig = "22 Rubber and Plastic" if nic_08_2dig == "22"
	replace nic_08_2dig = "23 Non-metallic minerals" if nic_08_2dig == "23"
	replace nic_08_2dig = "24 Basic Metals" if nic_08_2dig == "24"
	replace nic_08_2dig = "25 Fabricated Metals" if nic_08_2dig == "25"
	replace nic_08_2dig = "26 Computer and Electronics" if nic_08_2dig == "26"
	replace nic_08_2dig = "27 Electrical Equipment" if nic_08_2dig == "27"
	replace nic_08_2dig = "28 Machinery" if nic_08_2dig == "28"
	replace nic_08_2dig = "29 Motor Vehicles" if nic_08_2dig == "29"

expand 3
sort nic_08_2dig
foreach v in  theta_l_hat_q_ind_mean  theta_k_hat_q_ind_mean theta_m_hat_q_ind_mean theta_e_hat_q_ind_mean  {
	replace `v' = . if `v' == `v'[_n-1] | `v' == `v'[_n-2]
	tostring `v', replace format(%9.3f) force
	replace `v' = "" if `v' == "."
	}

replace nic_08_2dig = "" if nic_08_2dig == nic_08_2dig[_n-1] | nic_08_2dig == nic_08_2dig[_n-2] 
replace Observations = . if Observations == Observations[_n-1] | Observations == Observations[_n-2]
tostring Observations, replace format(%9.0f) force
replace Observations = "" if Observations == "."

foreach v in theta_l_hat_q_ind_sd theta_k_hat_q_ind_sd theta_m_hat_q_ind_sd theta_e_hat_q_ind_sd {
	replace `v' = . if !missing(nic_08_2dig)
	replace `v' = . if `v' == `v'[_n-1] 
	tostring `v', replace format(%9.3f) force
	replace `v' = "[" + `v' + "]"
	replace `v' = "" if `v' == "[.]"
	}

replace theta_l_hat_q_ind_mean = theta_l_hat_q_ind_sd if theta_l_hat_q_ind_mean == ""
replace theta_k_hat_q_ind_mean = theta_k_hat_q_ind_sd if theta_k_hat_q_ind_mean == ""
replace theta_m_hat_q_ind_mean = theta_m_hat_q_ind_sd if theta_m_hat_q_ind_mean == ""
replace theta_e_hat_q_ind_mean = theta_e_hat_q_ind_sd if theta_e_hat_q_ind_mean == ""
drop  theta_l_hat_q_ind_sd theta_k_hat_q_ind_sd theta_m_hat_q_ind_sd theta_e_hat_q_ind_sd  

texsave  nic_08_2dig theta_l_hat_q_ind_mean  theta_k_hat_q_ind_mean theta_m_hat_q_ind_mean theta_e_hat_q_ind_mean  Observations using "$tables/elasticities_`theta'.tex", ///
	footnote("Note: For each sector, the first row is the mean, second row, in brackets, is the standard deviation.") ///
	title("Output elasticities (`theta')") ///
	nofix replace location(h)  varlab marker(tab:elasticities_`theta') frag ///
	width(\textwidth)
}
